package com.bing.bingPro.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bing.bingPro.controller.dto.TOrderDateDTO;
import com.bing.bingPro.controller.dto.TTtComplainStatsProvinceDto;
import com.bing.bingPro.controller.vo.TTtComplainStatsCityVo;
import com.bing.bingPro.entity.TTtComplainStats;
import com.bing.bingPro.enums.TimeGranularityEnums;
import com.bing.bingPro.mapper.TTtComplainStatsMapper;
import com.bing.bingPro.service.TTtComplainStatsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author 29821
 * @description 针对表【t_tt_complain_stats(铁通投诉工单统计表)】的数据库操作Service实现
 * @createDate 2023-05-19 14:13:21
 */
@Service
public class TTtComplainStatsServiceImpl extends ServiceImpl<TTtComplainStatsMapper, TTtComplainStats>
        implements TTtComplainStatsService {

    @Resource
    private TTtComplainStatsMapper tTtComplainStatsMapper;

    @Override
    public List<TTtComplainStatsCityVo> queryMaintenanceProvince(TTtComplainStatsProvinceDto tOrderYearDto) {
        String time = tOrderYearDto.getTime().replace("-", "");
        //时间粒度为月
        if (Objects.equals(TimeGranularityEnums.YEAR.getCode(), tOrderYearDto.getTimeGranularity())) {
            return baseMapper.selectList(Wrappers.lambdaQuery(TTtComplainStats.class)
                            .eq(TTtComplainStats::getDateTime, time)
                            .eq(CharSequenceUtil.isNotEmpty(tOrderYearDto.getProvinceName()), TTtComplainStats::getProvinceName, tOrderYearDto.getProvinceName())
                            .eq(TTtComplainStats::getDateType, "month")
                            .eq(CharSequenceUtil.isNotEmpty(tOrderYearDto.getAreaType()), TTtComplainStats::getAreaType, tOrderYearDto.getAreaType()))
                    .stream()
                    .map(tTtComplainStats -> {
                        TTtComplainStatsCityVo vo = new TTtComplainStatsCityVo();
                        BeanUtil.copyProperties(tTtComplainStats, vo);
                        vo.setDateTime(tOrderYearDto.getTime());
                        return vo;
                    })
                    .collect(Collectors.toList());
        }//
        //时间粒度为日
        return baseMapper.selectList(Wrappers.lambdaQuery(TTtComplainStats.class)
                        .eq(TTtComplainStats::getDateType, "day")
                        .eq(CharSequenceUtil.isNotEmpty(tOrderYearDto.getProvinceName()), TTtComplainStats::getProvinceName, tOrderYearDto.getProvinceName())
                        .eq(CharSequenceUtil.isNotEmpty(tOrderYearDto.getAreaType()), TTtComplainStats::getAreaType, tOrderYearDto.getAreaType())
                        .eq(TTtComplainStats::getDateTime, time))
                .stream()
                .map(tTtComplainStats -> {
                    TTtComplainStatsCityVo vo = new TTtComplainStatsCityVo();
                    BeanUtil.copyProperties(tTtComplainStats, vo);
                    vo.setDateTime(tOrderYearDto.getTime());
                    return vo;
                })
                .collect(Collectors.toList());

    }


    @Override
    public List<TTtComplainStatsCityVo> exportMaintenance(TTtComplainStatsProvinceDto tOrderYearDto) {
        List<TTtComplainStatsCityVo> complainStatsProvinceVos = this.queryMaintenanceProvince(tOrderYearDto);
        return complainStatsProvinceVos.stream()
                .map(tTtComplainStats -> {
                    TTtComplainStatsCityVo vo = new TTtComplainStatsCityVo();
                    BeanUtil.copyProperties(tTtComplainStats, vo);
                    vo.setDateTime(tOrderYearDto.getTime());
                    return vo;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<String> dateList(TOrderDateDTO tOrderDateDTO) {
        //日期-月
        return Optional.ofNullable(tOrderDateDTO)
                .map(TOrderDateDTO::getTimeGranularity)
                .filter("1"::equals)
                .map(granularity -> tTtComplainStatsMapper.dateMonth()
                        .stream()
                        .map(date -> new StringBuilder(date).insert(4, "-").toString())
                        .collect(Collectors.toList())
                )
                //日期-日
                .orElseGet(() -> tTtComplainStatsMapper.dateDay()
                        .stream()
                        .map(dateStr -> LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("yyyyMMdd")))
                        .map(date -> date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")))
                        .collect(Collectors.toList())
                );
    }
}




